﻿
#region Usings
using System;

using PolyGameEngine.Tools;


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

#endregion

namespace PolyGameEngine.MenuSystem
{
    public class LevelSelect
    {

        public delegate void LevelSelected(string LevelName);
        public event LevelSelected LevelSelectedEvent;

        GameState gameState
        {
            get { return PhysicsGame.gamestate; }
            set { PhysicsGame.ChangeGameState(value); }
        }

        Viewport viewport;
        SpriteFont[] font;
        Texture2D background;
        Texture2D oshadow;
        int offset = 0;
        int j = 2;
        public string[] levels;
        float K = 0.3f;
        bool grow = true;
        ContentManager content;

        public LevelSelect(ContentManager Content, Viewport Viewport, SpriteFont[] Font)
        {
            
                levels = new string[1];
                levels[0] = "No Levels In Folder";
            content = Content;
            viewport = Viewport;
            numberOfLevelsToDisplay = (int)(viewport.Height - viewport.Height * 0.2f) / 25;
            font = Font;
            background = content.Load<Texture2D>("Menu/water");
            oshadow = content.Load<Texture2D>("Menu/OuterShadow");

        }


        public void update()
        {
            InputMan.Update();
            if (InputMan.OnPress(Keys.Escape))
            {
                AudioMan.PlayCue("Trampoline");
                gameState = GameState.Menu;
            }

            if (K >= 1f)
                grow = false;
            if (K <= 0.8f)
                grow = true;
            if (grow)
                K += 0.005F;
            else
                K -= 0.005f;

            if (InputMan.OnPress(Keys.S))
            {

                AudioMan.PlayCue("Menu Switch");
                if (++offset > levels.Length - 1)
                {
                    offset = 0;
                }
            }

            if (InputMan.OnPress(Keys.W))
            {

                AudioMan.PlayCue("Menu Switch");
                if (--offset < 0)
                {
                    offset = 0;
                }
            }

            if (InputMan.OnPress(Keys.Enter))
            {
                AudioMan.PlayCue("Menu Select");

                if (levels[offset] == "No Levels In Folder")
                    gameState = GameState.Menu;
                else
                    if (LevelSelectedEvent != null)
                    {
                        if (AudioMan.getPause())
                            AudioMan.PauseMusicOff();
                        AudioMan.menuStop();
                        AudioMan.PlayMusic();
                        LevelSelectedEvent(levels[offset]);
                        gameState = GameState.Play;
                    }
            }
        }


        int numberOfLevelsToDisplay;
        public void draw(SpriteBatch sb)
        {

            sb.Draw(background, new Rectangle(0, 0, background.Width, background.Height), Color.White);
            sb.Draw(oshadow, new Rectangle(0, 0, oshadow.Width, oshadow.Height), Color.White);
            Vector2 coordinates = new Vector2((int)(viewport.Width * 0.1), viewport.Height * 0.1f );

            if (j + offset >= levels.Length)
                j--;
            if (offset == 0)
                j = Math.Min( numberOfLevelsToDisplay, levels.Length);

            for (int i = offset; i < Math.Min(offset + numberOfLevelsToDisplay, levels.Length); i++)
            {

                if (levels.Length != 0)
                {
                    if (offset == i)
                        sb.DrawString(font[0], levels[i], coordinates, Color.Red, 0, Vector2.Zero, K, SpriteEffects.None, 0);
                    else
                        sb.DrawString(font[0], levels[i], coordinates, Color.Black);
                }

                coordinates.Y += 25;



            }

            coordinates.Y = viewport.Height;


        }





    }
}
